﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Threading;

namespace xmlTranslate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            openFileDialog1.Filter = "文本文件(*.xml)|*.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未选择文件");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("未设置要翻译的节点");
                return;
            }
            Thread t = new Thread(translate);
            t.IsBackground = true;
            t.Start();
        }

        private void translate()
        {
            ITranslate translate = new BaiduAPI();//new GoogleTranslate();
            try
            {
                List<string> str = textBox2.Text.Split('*').ToList();
                //定义并从xml文件中加载节点（根节点）
                XElement rootNode = XElement.Load(textBox1.Text);
                //查询语句: 获得根节点下name子节点（此时的子节点可以跨层次：孙节点、重孙节点......）
                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants()
                                                    from p in str
                                                    where target.Name == p
                                                    select target;
                progressBar1.Maximum = targetNodes.Count();
                int i = 1;//翻译接口调用太频繁会锁住
                foreach (XElement node in targetNodes.ToList())
                {
                    if (node.Value != "")
                    {
                        node.SetValue(translate.Translate(node.Value));
                    }
                    progressBar1.Value++;
                    label3.Text = "进度：" + $"{i}/{targetNodes.Count()}";
                        //+ ((double)progressBar1.Value * (double)100 / (double)progressBar1.Maximum) + "%";
                    i++;
                    //Thread.Sleep(1000);
                    //if (i > 50)
                    //{
                    //    i = 0;
                    //    Thread.Sleep(10000);//休眠10秒
                    //}
                }
                //SaveFileDialog saveFileDialog = new SaveFileDialog();
                //if (saveFileDialog.ShowDialog() == DialogResult.OK)
                //{
                //    rootNode.Save(saveFileDialog.FileName);
                //}
                rootNode.Save(textBox1.Text);
                progressBar1.Value = 0;
                MessageBox.Show("翻译完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
